package com.szly.phm.admin.controller;


import com.szly.phm.admin.aspectj.annotation.OperationLogAnnotation;
import com.szly.phm.admin.aspectj.enums.ModuleEnum;
import com.szly.phm.admin.aspectj.enums.OperationActionEnum;
import com.szly.phm.admin.utils.CustomResponse;
import com.szly.phm.admin.utils.SecurityUtils;
import com.szly.phm.common.entity.dto.parameter.*;
import com.szly.phm.common.entity.dto.user.LoginUserDTO;
import com.szly.phm.common.entity.result.PageResult;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.entity.vo.parameter.ParameterTypeVO;
import com.szly.phm.common.entity.vo.parameter.ParameterVO;
import com.szly.phm.common.service.ISysParameterService;
import com.szly.phm.common.service.ISysParameterTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lenovo.jason
 * @since 2021-10-10
 */
@RestController
@Validated
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/pc/parameter")
@Api(tags = "参数类型管理")
public class SysParameterController {
    private final ISysParameterTypeService parameterTypeService;

    private final ISysParameterService parameterService;
    // implement parameter 
    @ApiOperation(value = "分页查询参数类型")
    @GetMapping("/v1/parameter")
    @CustomResponse
    public RootResponse<PageResult<ParameterVO>> queryParameterPagev1(@Valid ParameterDTO parameterDTO){
        return parameterService.queryParameterPage(parameterDTO);
    }// end the query method

    // create
    @ApiOperation(value = "添加参数类型")
    @CustomResponse
    @OperationLogAnnotation(getModuleName = ModuleEnum.OTHER, getOperationAction = OperationActionEnum.SAVE)
    @PostMapping("/v1/parameter")
    public RootResponse createParameterV1(@Valid @RequestBody CreateParameterDTO createParameterDTO) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return parameterService.createParameter(createParameterDTO, userDTO);
    }// end the create method
    // update
    @ApiOperation(value = "修改参数类型")
    @CustomResponse
    @OperationLogAnnotation(getModuleName = ModuleEnum.OTHER, getOperationAction = OperationActionEnum.UPDATE)
    @PutMapping("/v1/parameter")
    public RootResponse updateParameterV1(@Valid @RequestBody UpdateParameterDTO updateParameterDTO) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return parameterService.updateParameter(updateParameterDTO, userDTO);
    }// end the update method

    /**
     *
     * @param id : parameter id here;
     * @return
     */
    @ApiOperation(value = "删除参数类型")
    @OperationLogAnnotation(getModuleName = ModuleEnum.OTHER, getOperationAction = OperationActionEnum.DELETE)
    @ApiImplicitParam(name = "id", value = "参数Id", dataType = "String", required = true, dataTypeClass = String.class)
    @DeleteMapping("/v1/parameter")
    @CustomResponse
    public RootResponse deleteParameterV1(@NotBlank(message = "角色Id不能为空") @RequestParam(value = "id") String id) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return parameterService.deleteParameter(id, userDTO);
    }// end the delete method

    @ApiOperation(value = "分页查询参数类型")
    @GetMapping("/v1/parameterType")
    @CustomResponse
    public RootResponse<PageResult<ParameterTypeVO>> queryParameterTypePagev1(@Valid ParameterTypeDTO parameterTypeDTO){
        return parameterTypeService.queryParameterTypePage(parameterTypeDTO);
    }// end the query method

    // create
    @ApiOperation(value = "添加参数类型")
    @OperationLogAnnotation(getModuleName = ModuleEnum.OTHER, getOperationAction = OperationActionEnum.SAVE)
    @PostMapping("/v1/parameterType")
    @CustomResponse
    public RootResponse createParameterTypeV1(@Valid @RequestBody CreateParameterTypeDTO createParameterTypeDTO) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return parameterTypeService.createParameterType(createParameterTypeDTO, userDTO);
    }// end the create method
    // update
    @ApiOperation(value = "修改参数类型")
    @OperationLogAnnotation(getModuleName = ModuleEnum.OTHER, getOperationAction = OperationActionEnum.UPDATE)
    @PutMapping("/v1/parameterType")
    @CustomResponse
    public RootResponse updateParameterTypeV1(@Valid @RequestBody UpdateParameterTypeDTO updateParameterTypeDTO) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return parameterTypeService.updateParameterType(updateParameterTypeDTO, userDTO);
    }// end the update method

    /**
     *
     * @param id : parameterType id here;
     * @return
     */
    @ApiOperation(value = "删除参数类型")
    @OperationLogAnnotation(getModuleName = ModuleEnum.OTHER, getOperationAction = OperationActionEnum.DELETE)
    @ApiImplicitParam(name = "id", value = "参数Id", dataType = "String", required = true, dataTypeClass = String.class)
    @DeleteMapping("/v1/parameterType")
    @CustomResponse
    public RootResponse deleteParameterTypeV1(@NotBlank(message = "角色Id不能为空") @RequestParam(value = "id") String id) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return parameterTypeService.deleteParameterType(id, userDTO);
    }// end the delete method
}

